(function(){
    angular.module('app').controller("agbDealCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.isDirect = isDirect;
        $scope.typeList = [{name:"全部资源类型"},{name:"保价保量",id:"1"},{name:"保价不保量",id:"2"}];
        $scope.filter = {}
        $scope.targetList = [
            {
                title: '合约交易(保价不保量)当日花费(元)',
                t: 'pdSpend',
                yt: 'pdYSpend',
                ytb: 'pdYbSpend',
            },{
                title: '合约交易(保价不保量)当日利润(元)',
                t: 'pdProfit',
                yt: 'pdYProfit',
                ytb: 'pdYbProfit',
            },{
                title: '合约交易(保价不保量)当日利润率',
                t: 'pdProfitRate',
                yt: 'rtbYProfitRate',
                ytb: 'rtbYbProfitRate',
            },{
                title: '合约交易(保价保量)当日收入(元)',
                t: 'pdbIncome',
                yt: 'pdbYIncome',
                ytb: 'pdbYbIncome',
            }
        ];
        $scope.$watch('permission',function (newValue, oldValue) {
            if(angular.equals({},newValue)) return;
            setTatgetList(newValue)
        });
        $scope.$on('permission',function (e, permission) {
            setTatgetList(permission)
        });
        // $scope.sTime = getNowFormatDate() + " 00:00:00";
        // $scope.eTime = getNowFormatDate() + " 23:59:59";
        $scope.chooseDate = function (start, end) {
            if(start) {
                $scope.sTime = start + " 00:00:00";
                $scope.eTime = end + " 23:59:59";
            } else {
                $scope.sTime = $scope.eTime = undefined;
            }
            getList();
            getSum();
        }
        $scope.selectType = function (item) {
            $scope.filter.DealType = item.id;
            getList();
            getSum();
        }
        $scope.selectAdv = function (item) {
            $scope.filter.advertiseID = item.id;
            getList();
            getSum();
        }

        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/deal/sku',
                load: true,
                data: data,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        v.dealType = v.dealType == "pdb"?"保价保量":"保价不保量";
                        v.clickRate = (v.clickRate * 100).toFixed(2) + "%";
                    })
                    exportCsv({
                        title: ['广告主名称','资源类型','成本(元)','收入(元)','单价(元)','展示量','点击量','点击率'],
                        titleForKey: ['skuName','dealType','dspCost','agencyProfit','price','view','click','clickRate'],
                        data: arr,
                    },"合约交易概况.csv");
                }
            })
        }

        // $scope.getRowspan = function(id) {
        //     var count = 0;
        //     $scope.list.forEach(function(val){
        //         if(val.advertiserID == id) count++;
        //     })
        //     return count;
        // }

        getList();
        getSum();
        getTarget();
        getAdvList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/deal/sku',
                load: true,
                data: data,
                success: function(res){
                    var arr = [];
                    // 获得列表
                    var list = res.data.data || [];
                    for(var i = 0;i < list.length; i++) {
                        arr.push(list[i])
                        if(!list[i].dealType) {
                            var pdTr = angular.copy(list[i]);

                            list[i].dealType = 'pdb'
                            pdTr.dealType = 'pd';
                            arr.push(pdTr);
                        }
                    }
                    $scope.list = arr;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }
        function getSum() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/deal/sku/sum',
                load: true,
                data: {
                    'page': "1",
                    'size': "999999",
                    'filter': $scope.filter,
                    sTime : $scope.sTime,
                    eTime : $scope.eTime
                },
                success: function(res){
                    // 获得列表
                    $scope.total = res.data.data;
                }
            })
        }
        // 指标
        function getTarget() {
            // 默认今日
            var time = getNowFormatDate();
            var data = {
                'filter': {},
                sTime : time + " 00:00:00",
                eTime : time + " 23:59:59"
            };
            // 竞价交易总额
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agency/main',
                data: data,
                success: function(res){
                    $scope.target = res.data.data
                    $scope.target.pdProfitRate = ($scope.target.pdProfitRate*100).toFixed(2)+"%";
                    $scope.target.rtbYProfitRate = ($scope.target.rtbYProfitRate*100).toFixed(2)+"%";
                    $scope.target.rtbYbProfitRate = ($scope.target.rtbYbProfitRate*100).toFixed(2)+"%";
                }
            })
        }

        // 广告主
        function getAdvList() {
            var data = {
                'page': "1",
                'size': "9999",
                "sort": ["cTime,1"],
                "filter": $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/putin/list',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.advList = res.data.data;
                    $scope.advList.unshift({name:"全部广告主"})
                }
            })
        }
        // 根据权限变更指标
        function setTatgetList(permission) {
            var targetList = $scope.targetList;
            if(!permission.view['报表中心'].sub['合约交易概况'].sub || !permission.view['报表中心'].sub['合约交易概况'].sub['成本收入']) {
                // 移除利润相关
                for(var i = 0; i < targetList.length; i++) {
                    if(targetList[i].t == 'pdProfit') {
                        targetList.splice(i,3);
                        break;
                    }
                }
            }
            $scope.permission = permission;
        }
    })
})();